<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="10" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar :user="user" />
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item" v-if="mallInfo != null">
                <svg-icon icon-class="peoples" />商城名称
                <div class="pull-right">{{ mallInfo.mallName }}</div>
              </li>
              <li
                class="list-group-item"
                v-if="mallInfo != null"
                style="display: flex; align-items: center"
              >
                <svg-icon icon-class="peoples" />商城用户端入口链接
                <div class="pull-right" style="margin: 0 10px">
                  {{ `${mallInfo.mallEntryPath}?mall_id=${mallInfo.id}` }}
                </div>
                <el-popover placement="right" width="240" trigger="click">
                  <div>
                    <canvas ref="qrcodeCanvas"></canvas>
                  </div>
                  <el-button
                    type="primary"
                    size="mini"
                    slot="reference"
                    @click="
                      handleNhUrl(
                        `${mallInfo.mallEntryPath}?mall_id=${mallInfo.id}`
                      )
                    "
                    >生成</el-button
                  >
                </el-popover>
              </li>
              <li
                class="list-group-item"
                v-if="mallInfo != null"
                style="
                  display: flex;
                  align-items: center;
                  justify-content: space-between;
                "
              >
                <div><svg-icon icon-class="peoples" />小程序端入口链接</div>
                <div class="pull-right">
                  <el-button
                    type="primary"
                    size="mini"
                    slot="reference"
                    @click="oncreateQRCode()"
                    >生成小程序码</el-button
                  >
                </div>
              </li>
              <li class="list-group-item" v-if="mallInfo != null">
                <svg-icon icon-class="peoples" />商城PC端入口链接
                <div class="pull-right">
                  {{ mallInfo.mallPcEntry }}
                </div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="14" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="生成二维码" :visible.sync="open" width="500px">
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="路径">
            <el-input
              v-model="form.xcxCodePath"
              placeholder="请填写路径"
            ></el-input>
          </el-form-item>
          <el-form-item label="版本">
            <el-radio-group v-model="form.xcxCodeVersion">
              <el-radio label="release">正式版</el-radio>
              <el-radio label="trial">体验版</el-radio>
              <el-radio label="develop">开发版</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div v-if="createImg" style="display: flex; justify-content: center">
          <img style="width: 200px" :src="createImg" alt="" />
        </div>
      </div>
      <div slot="footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="subLoad"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import { getUserProfile, getMallInfo, createQRCode } from "@/api/system/user";
import QRCode from "qrcode";
export default {
  name: "Profile",
  components: { userAvatar, userInfo, resetPwd },
  data() {
    return {
      user: {},
      createImg: "",
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      mallInfo: {},
      open: false,
      form: {},
      subLoad: false,
    };
  },
  created() {
    this.getUser();
    this.getMall();
  },
  methods: {
    //生成二维码
    handleNhUrl(url) {
      const canvas = this.$refs.qrcodeCanvas;
      QRCode.toCanvas(
        canvas,
        url,
        { width: 200, margin: 1 },
        (error, canvas) => {}
      );
    },
    oncreateQRCode() {
      this.createImg = "";
      this.open = true;
      this.form = {};
    },
    getCreateImg(data) {
      this.subLoad = true;
      createQRCode(data).then((response) => {
        this.createImg = response.filePath;
        this.subLoad = false;
      });
    },
    submitForm() {
      this.getCreateImg(this.form);
    },
    getUser() {
      getUserProfile().then((response) => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },
    getMall() {
      getMallInfo().then((res) => {
        this.mallInfo = res.data || null;
      });
    },
  },
};
</script>
<style scoped lang="scss">
.pull-right {
  max-width: 60%;
  word-wrap: break-word;
}
</style>
